package com.ease.web.struts2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StreamResult;

import com.ease.common.util.Assertion;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;

//<action name="myaction" class="com.example.MyAction">
//	<result name="resultName" type="workbook">
//		<param name="name">${fileName}</param>
//		<param name="workbook">${workbook}</param>
//	</result>
//</action>

/**
 * 
 * @author nathanleewei
 * 
 */
public class WorkbookResult extends StreamResult {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8650240900101770993L;

	private String format = "xls";

	private String name;

	private String workbook;

	private HSSFWorkbook workbookObject;

	protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {

		this.setContentType(format == "xlsx" ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "application/vnd.ms-excel");
		// this.setContentType(format == "xlsx" ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :
		// "application/vnd.ms-excel; charset=UTF-8");

		this.setContentCharSet(ServletActionContext.getRequest().getCharacterEncoding());
		this.setBufferSize(1024);

		name = this.conditionalParse(name, invocation);
		name = (null == name) ? "unnamed" : name;

		// finalLocation = new String(finalLocation.getBytes(),
		// ServletActionContext.getRequest().getCharacterEncoding());
		// finalLocation = new String(finalLocation.getBytes(), "ISO8859-1");

		name = java.net.URLEncoder.encode(name, "UTF8");
		// name = java.net.URLEncoder.encode(name, this.getContentCharSet());

		this.setContentDisposition(null);
		ServletActionContext.getResponse().addHeader("Content-Disposition", "attachment;filename=" + name + "." + format);
		// ServletActionContext.getResponse().addHeader("Content-Disposition",
		// "inline;filename=" + name + "." +
		// format);

		ValueStack stack = invocation.getStack();
		Object tempWorkbookObject = stack.findValue(this.workbook);
		Assertion.notNull(tempWorkbookObject, this.workbook + " not found");
		Assertion.isTrue((tempWorkbookObject instanceof HSSFWorkbook), this.workbook + " is not instanceof HSSFWorkbook");
		workbookObject = (HSSFWorkbook) tempWorkbookObject;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		workbookObject.write(baos);
		this.inputStream = new ByteArrayInputStream(baos.toByteArray());

		super.doExecute(finalLocation, invocation);

		this.inputStream = null;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the format
	 */
	public String getFormat() {
		return format;
	}

	/**
	 * @param format
	 *            the format to set
	 */
	public void setFormat(String format) {
		this.format = format;
	}

	/**
	 * @return the workbook
	 */
	public String getWorkbook() {
		return workbook;
	}

	/**
	 * @param workbook
	 *            the workbook to set
	 */
	public void setWorkbook(String workbook) {
		this.workbook = workbook;
	}

}
